﻿using System.ComponentModel;
using Medicalsystem.MCP.FormDesigner.Define;
using Medicalsystem.MCP.FormDesigner.Interface;

namespace Medicalsystem.MCP.FormDesigner.Property
{
    /// <summary>
    /// 继承 ICtrlBase 接口控件与属性编辑器交互
    /// </summary>
    public class GetCtrlProperty : GetProperty
    {
        /// <summary>
        /// 自定义控件属性基类
        /// </summary>
        /// <param name="Control"></param>
        public GetCtrlProperty(System.Windows.Forms.Control control) : base(control) { }

        /// <summary>
        /// 属性外部事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public delegate void PropertyHandler(object sender, string propertyName, object value);

        /// <summary>
        /// 批量设置控件属性
        /// </summary>
        public event PropertyHandler SetPropertyBatchEvent;

        /// <summary>
        /// 控件属性改变通知 - 当前应用于 控件 名称改变通知
        /// </summary>
        public event PropertyHandler PropertyChangeEvent;

        /// <summary>
        /// 控件名称
        /// </summary>
        [CategoryAttribute("设计")]
        [ShowChinese("ID")]
        [Description("指示模板中用来唯一标识该对象的名称")]
        public string ID
        {
            get { return ((ICtrlBase)_control).CtrlID; }
            set
            {
                if (value.Trim().Length == 0)
                {
                    System.Windows.Forms.MessageBox.Show("名称无效", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                }
                else
                {
                    CtrlMng tempCtr = (_control as ICtrlBase).Model.Controls.Ctrls.Find(delegate(CtrlMng ctrl) { return ctrl.Ctrl.CtrlID == value.Trim() && ctrl.Ctrl != _control; });

                    if (tempCtr != null)
                    {
                        System.Windows.Forms.MessageBox.Show("名称已占用", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    }
                    else
                    {
                        string name = ((ICtrlBase)_control).CtrlID;
                        ((ICtrlBase)_control).CtrlID = value.Trim();
                        if (PropertyChangeEvent != null)
                        {
                            PropertyChangeEvent(_control, "Name", name);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 事件是否绑定
        /// </summary>
        [Browsable(false)]
        public bool PropertyChangeEventBind
        {
            get { return PropertyChangeEvent == null ? false : true; }
        }

        /// <summary>
        /// 订阅属性批量修改,需要进行批量修改的属性,执行该方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        protected void OnSetPropertyBatch(object sender, string propertyName, object value)
        {
            if (this.SetPropertyBatchEvent != null)
            {
                SetPropertyBatchEvent(sender, propertyName, value);
            }
        }

        /// <summary>
        /// 设置控件属性 - 作为批量设置入口（如果属性允许批量选取批量设置，需要重写该方法）
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public virtual void SetProperty(string propertyName, object value) { }
    }
}